package com.youlin.untils;

import com.youlin.security.SecurityUser;
import com.youlin.entities.BygAdmin;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import java.util.List;

public class AdminUtil {

    public static BygAdmin getCurrentAdmin () {
        SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return securityUser.bygAdmin;
    }

    public static void setCurrentAdmin (SecurityUser userDetails) {
        SecurityContextHolder.getContext().setAuthentication(
                new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities()));
    }

    public static void removeUser (SessionRegistry sessionRegistry, Integer adminId) {
        List<Object> users = sessionRegistry.getAllPrincipals(); // 获取session中所有的用户信息
        for (Object principal : users) {
            if (principal instanceof SecurityUser) {
                final SecurityUser securityUser = (SecurityUser) principal;
                if (securityUser.getUserId().equals(adminId)) {
                    List<SessionInformation> sessionsInfo = sessionRegistry
                            .getAllSessions(principal, false); // false代表不包含过期session
                    if (MyBeanUtil.isNotEmpty(sessionsInfo)) {
                        for (SessionInformation sessionInformation : sessionsInfo) {
                            sessionInformation.expireNow();
                        }
                    }
                }
            }
        }
    }

    public static void onlyUser (SessionRegistry sessionRegistry, Integer adminId, String setCookie) {
        List<Object> users = sessionRegistry.getAllPrincipals(); // 获取session中所有的用户信息
        for (Object principal : users) {
            if (principal instanceof SecurityUser) {
                final SecurityUser securityUser = (SecurityUser) principal;
                if (securityUser.getUserId().equals(adminId)) {
                    List<SessionInformation> sessionsInfo = sessionRegistry
                            .getAllSessions(principal, false); // false代表不包含过期session
                    if (MyBeanUtil.isNotEmpty(sessionsInfo)) {
                        for (SessionInformation sessionInformation : sessionsInfo) {
                            String sessionId = sessionInformation.getSessionId();
                            if (! setCookie.contains(sessionId)) sessionInformation.expireNow();
                        }
                    }
                }
            }
        }
    }

    public static boolean findUser (SessionRegistry sessionRegistry, String JSESSIONID) {
        boolean flag = false;
        List<Object> users = sessionRegistry.getAllPrincipals(); // 获取session中所有的用户信息
        for (Object principal : users) {
            if (principal instanceof SecurityUser) {
                List<SessionInformation> sessions = sessionRegistry
                        .getAllSessions(principal, false); // false代表不包含过期session
                flag = sessions.parallelStream().anyMatch(session -> session.getSessionId().equals(JSESSIONID));
                if (flag) break;
            }
        }
        return flag;
    }

}
